<?php
/**
* @version		$Id: router.php 9764 2007-12-30 07:48:11Z ircmaxell $
* @package		Joomla
* @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

function oscommerceBuildRoute(&$query)
{
$GLOBALS['database'] = $database = JFactory::getDBO();
	$segments = array();

	if(isset($query['osMod']))
	{
		
			$segments[] = $query['osMod'];

		

		unset($query['osMod']);
	}else{
	
      $segments[] = 'index';
	}
  
  if(isset($query['catID'])){
  $catid_arr = explode("_", $query['catID']);
  $catid_cnt = count($catid_arr);
  $dbidc = $catid_cnt - 1;
  $dbid = $catid_arr[$dbidc];
  $database->setQuery("Select categories_name from #__osc_categories_description where categories_id='$dbid'");
  $result = $database->loadObject();
  //print_r($result);
  //$database->query();
  //$catidb = $database->loadObjectList();
  //print_r($catidb);
  //ok lets get rid of some aspects:
  $catname = str_replace("_", "-", $result->categories_name);
  $catname = str_replace(" ", "-", $catname);
  $catname = str_replace("\"", "", $catname);
  $catname = str_replace("'","",$catname);
  $catname = str_replace("&amp;","-", $catname);
  $catname = str_replace("&", "-", $catname);
  $catname = str_replace("+","", $catname);
  
      $segments[] = 'cat_' . $catname;
//unset($query['catID']);
  }
  
  if(isset($query['products_id'])){
  $dbid = $query['products_id'];
  //echo $dbid;
  $database->setQuery("Select products_name from #__osc_products_description where products_id='$dbid'");
  $result = $database->loadObject();
  $prodname = str_replace("_", "-", $result->products_name);
  $prodname = str_replace(" ", "-", $prodname);
      $segments[] = 'prd_' . $prodname;
 
  }
	return $segments;
}

function oscommerceParseRoute($segments)
{
	$vars = array();

	//Get the active menu item
	$menu =& JSite::getMenu();
	$item =& $menu->getActive();

	// Count route segments
	$count = count($segments);

	//Standard routing for articles
//	if(!isset($item))
//	{
		//$vars['osMod']  = $segments[0];
		//we need for to find which var we working on
		//so lets loop through them
		foreach ($segments as $val){
		$mod = explode("_", $val);
		//print_r($mod);
		if (count($mod) ==1) {
		$vars['osMod'] = $val;
		//echo $mod[0];
		}else{
		//k now comes the fun!!!
		  if($mod[0] == 'prd'){
		  //product id -- to do
		  }elseif($mod[0] == 'cat'){
		  //catid -- to do

		  
		  

		  
		  
		  }else{
		  //osmod
		  $vars['osMod'] = $val;
		  }
		}
		}
		
		
		//$vars['id']    = $segments[$count - 1];
		
//		return $vars;
//	}
//print_r($segments);
//print_r($vars);
	return $vars;
}
?>